<?php
require_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/app.php');

need_manager();
//need_auth('market');

$id = abs(intval($_REQUEST['id']));
$banner = Table::Fetch('banner', $id);

$table = new Table('banner', $_POST);
$uarray = array('title','image','begin_time','end_time','display','sort_order','custom','position',); 
$table->display = strtoupper($table->display)=='Y' ? 'Y' : 'N';
$table->image = upload_image('upload_image', $banner['image'], 'team');
$table->begin_time = strtotime($_POST['begin_time']);
$table->end_time = strtotime($_POST['end_time']);

if (!$_POST['title'] ) {
	Session::Set('error', 'banner标题不能为空');
	redirect(null);
}

if ( $banner ) {
	if ( $flag = $table->update( $uarray ) ) {
		Session::Set('notice', '编辑banner成功');
	} else {
		Session::Set('error', '编辑banner失败');
	}
} else {
	if ( $flag = $table->insert( $uarray ) ) {
		Session::Set('notice', '新建banner成功');
	} else {
		Session::Set('error', '新建banner失败');
	}
}

redirect(null);
